<?php

namespace SummoIngenio\Sai\MatematicaBundle\Utility\Juego\P0;
use SummoIngenio\Sai\MatematicaBundle\Utility\Juego\JuegoBase;
define('CANT_PROBLEMAS', 5);
/**
 * Description of P01060	
 *
 * @author SVM
 */

class P01060 extends JuegoBase {
  public $objeto;
  public $obj;
  public function init() {
	$s = rand(0,1);
	if($s==1){
		$this->suma();
	}
	if($s==0){
		$this->resta();
	}	
  }
  function resta(){
	$rsp = rand(1,17);
        
	$uni = array();
	for($i=$rsp+1; $i<20;$i++){
		array_push($uni,$i);
	}
	shuffle($uni);

	$op1 = $uni[0];
	$op2 = $op1-$rsp;

	
	$op3 = $uni[1];
	$op4 = $op3-$rsp;

	$op5 = $uni[2];
	$op6 = $op5-$rsp;
	$this->sig = '-';	

	$this->op1 = $op1;
	$this->op2 = $op2;
	$this->op3 = $op3;
	$this->op4 = $op4;
	$this->op5 = $op5;
	$this->op6 = $op6;
	
	$this->matrix = array(array(0,0,0),array(0,0,0),array(0,0,0));
	$x = 'x';
	$this->objeto = array(array(rand(1,20),rand(1,20),rand(1,20)),array(rand(1,20),rand(1,20),rand(1,20)),array(rand(1,20),rand(1,20),rand(1,20)));	
        
	$this->rsp=$rsp;
		
	$coor = $this->generar_cor();
	while(!$this->Ingresar_numero($op1,$op2,$coor)){
		$coor = $this->generar_cor();
	}
	
        $coor = $this->generar_cor();	
	while(!$this->Ingresar_numero($op3,$op4,$coor)){
		$coor = $this->generar_cor();
	}
	$coor = $this->generar_cor();	
	while(!$this->Ingresar_numero($op5,$op6,$coor)){
		$coor = $this->generar_cor();
	}   
  }
  function suma(){
	$rsp = rand(6,20);
	$uni = array();
	for($i=1; $i<$rsp;$i++){
		array_push($uni,$i);
	}
	shuffle($uni);

	$op1 = $uni[0];
	$op2 = $rsp-$op1;
	
	$j = 1;
	for($s=1;$s<$rsp;$s++){
		if($uni[$s]!=$op2){
			$j = $s;
			break;
		}
	}
	
	$op3 = $uni[$j];
	$op4 = $rsp-$op3;
	$j++;
	for($s=$j;$s<$rsp;$s++){
		if($uni[$s]!=$op4){
			$j = $s;
			break;
		}
	}	
	$op5 = $uni[$j];
	$op6 = $rsp-$op5;
	
	$this->sig = '+';

	$this->op1 = $op1;
	$this->op2 = $op2;
	$this->op3 = $op3;
	$this->op4 = $op4;
	$this->op5 = $op5;
	$this->op6 = $op6;

        
	$this->matrix = array(array(0,0,0),array(0,0,0),array(0,0,0));
	$x = 'x';
	$this->objeto = array(array(rand(1,20),rand(1,20),rand(1,20)),array(rand(1,20),rand(1,20),rand(1,20)),array(rand(1,20),rand(1,20),rand(1,20)));	
        
	$this->rsp=$rsp;
		
	$coor = $this->generar_cor();
	while(!$this->Ingresar_numero($op1,$op2,$coor)){
		$coor = $this->generar_cor();
	}
	
        $coor = $this->generar_cor();	
	while(!$this->Ingresar_numero($op3,$op4,$coor)){
		$coor = $this->generar_cor();
	}
	$coor = $this->generar_cor();	
	while(!$this->Ingresar_numero($op5,$op6,$coor)){
		$coor = $this->generar_cor();
	}  
  }
  
  function generar_cor(){
	return array(rand(0,2),rand(0,2));
  }  
  public function Ingresar_numero($v1,$v2,$co){
	if($this->verificar_espacio($co)){
		if($this->ingresar($v1,$co)){
			if($this->ingresar_ad($v2,$co)){
				return true;
			}
			if($this->ingresar_at($v2,$co)){
				return true;
			}
			if($this->ingresar_ar($v2,$co)){
				return true;
			}
			if($this->ingresar_ab($v2,$co)){
				return true;
			}							
			return false;
		}else{
			return false;
		}
	}
	return false;
  }
  public function ingresar_ad($vl,$cords){
    if($cords[0]<2){
		if($this->matrix[$cords[1]][$cords[0]+1]==0){
			$this->matrix[$cords[1]][$cords[0]+1] = 1;
			$this->objeto[$cords[1]][$cords[0]+1] = $vl;
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}	
  }
  public function ingresar_at($vl,$cords){
    if($cords[0]>0){
		if($this->matrix[$cords[1]][$cords[0]-1]==0){
			$this->matrix[$cords[1]][$cords[0]-1] = 1;
			$this->objeto[$cords[1]][$cords[0]-1] = $vl;
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
  }
  public function ingresar_ar($vl,$cords){
    if($cords[1]>0){
		if($this->matrix[$cords[1]-1][$cords[0]]==0){
			$this->matrix[$cords[1]-1][$cords[0]] = 1;
			$this->objeto[$cords[1]-1][$cords[0]] = $vl;
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
  }
  public function ingresar_ab($vl,$cords){
    if($cords[1]<2){
		if($this->matrix[$cords[1]+1][$cords[0]]==0){
			$this->matrix[$cords[1]+1][$cords[0]] = 1;
			$this->objeto[$cords[1]+1][$cords[0]] = $vl;
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
  }     
  public function ingresar($val,$cords){
	if($this->matrix[$cords[1]][$cords[0]]==0){ 
		$this->matrix[$cords[1]][$cords[0]] = 1;
		$this->objeto[$cords[1]][$cords[0]] = $val;
		return true;		
	}else{
		return false;
	}
  }
  public function verificar_espacio($cords){
	if($this->matrix[$cords[1]][$cords[0]]==0){
		return true;
	}else{
		return false;
	}
  }
  public function getPregunta() {
    return '';
  }
  public function getHtmlDesactivado() {	
    return $this->render('P0/P01060/desactivado', array(
        'resp'   =>  $this->rsp,		
        'objeto'   =>  $this->objeto,
		'sig' => $this->sig,
      ));
  }
  
  public function getHtmlJugable() {
      return $this->render('P0/P01060/jugable', array(  
        'resp'   =>  $this->rsp,		
        'objeto'   =>  $this->objeto,
		'sig' => $this->sig,
      ));
  }
  
  public function getHtmlResuelto() {
    return $this->render('P0/P01060/resuelto', array(
        'resp'   =>  $this->rsp,		
        'objeto'   =>  $this->objeto,
		'sig' => $this->sig,
		'op1' => $this->op1,
		'op2' => $this->op2,
		'op3' => $this->op3,
		'op4' => $this->op4,		
		'op5' => $this->op5,
		'op6' => $this->op6,		
      ));
  }
  
  public function verificarRespuesta($respuestaUsuario) {
         $rsp = true;
	 
	 $cadena1 = $respuestaUsuario->get('valor1')+$this->sig+$respuestaUsuario->get('valor2')+"="+$this->rsp+",";
         $cadena2 = $respuestaUsuario->get('valor3')+$this->sig+$respuestaUsuario->get('valor4')+"="+$this->rsp+",";	 
	 $cadena3 = $respuestaUsuario->get('valor5')+$this->sig+$respuestaUsuario->get('valor6')+"="+$this->rsp+",";	 
	 $cadena = $cadena1+$cadena2+$cadena3;
	 $temp = $this->objeto;
       if($this->com_respuesta($respuestaUsuario->get('valor1'),$respuestaUsuario->get('valor2'),$temp)){
           if($this->rsp != $respuestaUsuario->get('valor7')){
                $rsp=false;
           }
	}
        
       if(!$this->com_respuesta($respuestaUsuario->get('valor3'),$respuestaUsuario->get('valor4'),$temp)){
           if($this->rsp != $respuestaUsuario->get('valor8')){
                $rsp=false;
           }
	 }
       if(!$this->com_respuesta($respuestaUsuario->get('valor5'),$respuestaUsuario->get('valor6'),$temp)){
           if($this->rsp != $respuestaUsuario->get('valor9')){
                $rsp=false;
           }
	 }

    $aRetornar = array(
      'respuesta' => $cadena,
      'correcta'  => $rsp,
    );
    return $aRetornar;
  }
  public function com_respuesta($v1,$v2,&$obj){
    if($this->sig=='-'){
		if($v1<$v2){return false;}
		if($v1-$v2!=$this->rsp){return false;}
    }else{
		if($v1+$v2!=$this->rsp){return false;}
    }
    $x = -1;
	$y = -1;
	for($j=0;$j<3;$j++){
		$fila = $obj[$j];
		for($i=0;$i<3;$i++){
			if($fila[$i]==$v1){
				$x = $j;
				$y = $i;
                               
                                if($this->comp_ar($v2,$x,$y,$obj)){
                                        $obj[$j][$i] = '';                                        
                                        return true;
                                }
                                if($this->comp_ab($v2,$x,$y,$obj)){
                                        $obj[$j][$i] = '';                                    
                                        return true;
                                }
                                if($this->comp_ad($v2,$x,$y,$obj)){
                                        $obj[$j][$i] = '';                                    
                                        return true;
                                }
                                if($this->comp_at($v2,$x,$y,$obj)){
                                        $obj[$j][$i] = '';                                    
                                        return true;
                                }                                
				
			}
		}
	}
        return false;

  }
  public function comp_ar($vl,$x,$y,&$obj){
	if($x>0){
		if($obj[$x-1][$y]==$vl){
                        $obj[$x-1][$y] = '';
			return true;
		}else{
			return false;
		}		
	}
	return false;
  }
  public function comp_ab($vl,$x,$y,&$obj){
	if($x<2){
		if($obj[$x+1][$y]==$vl){
                        $obj[$x+1][$y] = '';                    
			return true;
		}else{
			return false;
		}
	}
	return false;
  }
  public function comp_ad($vl,$x,$y,&$obj){
	if($y<2){
		if($obj[$x][$y+1]==$vl){
                        $obj[$x][$y+1] = '';                    
			return true;
		}else{
			return false;
		}
	}
	return false;
  }
  public function comp_at($vl,$x,$y,&$obj){
	if($y>0){
		if($obj[$x][$y-1]==$vl){
                        $obj[$x][$y-1] = '';                    
			return true;
		}else{
			return false;
		}
	}
	return false;
  }   
  public function getJavascript() {
    $aRetornar = array(
      "tipo"      => "archivo",
      "contenido" => "P0/P01060.js"
    );
    return $aRetornar;
  }

  public function getCss() {
    $aRetornar = array(
      "tipo"      => "archivo",
      "contenido" => "P0/P01060.css"
    );
    return $aRetornar;
  }

  public function __sleep() {
    return array(
        'rsp',		
        'objeto',     
		'sig',
		'op1',
		'op2',
		'op3',
		'op4',
		'op5',
		'op6',
    );
  }
}

?>